package com.geek.video.exception;

import javax.servlet.http.HttpServletRequest;

import com.geek.video.enums.ResponseEnum;
import com.geek.video.web.response.ResponseData;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import reactor.core.publisher.Mono;

/**
 * 全局controller层异常处理
 *
 * @author qinxh
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = Exception.class)
    public Mono<ResponseData> exceptionHandler(HttpServletRequest request, Exception e) {

        logger.error(e.getMessage());
        if (e instanceof GlobalException) {
            GlobalException globalException = (GlobalException) e;
            return ResponseData.ofStatus(globalException.getmessage());
        }

        return ResponseData.ofStatus(ResponseEnum.SERVER_ERROR);
    }
}